#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<windows.h>
#define high 20
#define width 30
//全局变量
int canvas[high][width];//0为空格，-1边框，1蛇头，大于一蛇身
int movedirection;//1,2,3,4分别表示小蛇上下左右移动
int food_x, food_y;//记录食物位置，-2输出食物
//光标移动到x,y位置
void gotoxy(int x, int y) {
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD pos;
	pos.X = x;
	pos.Y = y;
	SetConsoleCursorPosition(handle, pos);
}
//第二个值为0表示隐藏光标
void HideCursur() {
	CONSOLE_CURSOR_INFO cursor_info = { 1,0 };
	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
void movesnakebydirection() {
	int i, j;
	int max = 0;
	int oldtail_i, oldtail_j;//记录旧的蛇尾
	int oldhead_i, oldhead_j;//记录旧的蛇头位置
	for (i = 1; i < high - 1; i++) {
		for (j = 1; j < width - 1; j++) {
			if (canvas[i][j] > 0) {
				canvas[i][j]++;//对所有大于0的元素加一
				if (max < canvas[i][j]) {
					max = canvas[i][j];//求出最大值
					oldtail_i = i;
					oldtail_j = j;
				}
				if (canvas[i][j] == 2) {
					oldhead_i = i;
					oldhead_j = j;
				}
			}
		}
	}
	int newhead_i, newhead_j;
	if (movedirection == 1) {//向上
		newhead_i = oldhead_i - 1;
		newhead_j = oldhead_j;
	}
	if (movedirection == 2) {//向下
		newhead_i= oldhead_i + 1;
		newhead_j = oldhead_j;
	}
	if (movedirection == 3) {//向左
		newhead_i = oldhead_i;
		newhead_j = oldhead_j - 1;
	}
	if (movedirection == 4) {//向右
		newhead_i = oldhead_i;
		newhead_j = oldhead_j + 1;
	}
	//判断小蛇蛇头是否碰到食物
	if (canvas[newhead_i][newhead_j] == -2) {
		canvas[food_x][food_y] = 0;//食物原始位置归0，产生新的食物
		food_x = rand() % (high - 5) + 2;
		food_y = rand() % (width - 5) + 2;
		canvas[food_x][food_y] = -2;
	}
	else {
		canvas[oldtail_i][oldtail_j] = 0;//没有吃到食物，长度不变
	}
	//判断小蛇蛇头撞到边框或自身，游戏失败
	if (canvas[newhead_i][newhead_j] > 0 || canvas[newhead_i][newhead_j] == -1) {
		printf("游戏失败");
		exit(0);
	}
	else {
		canvas[newhead_i][newhead_j] = 1;
	}
}
//数据初始化
void startup() {
	memset(&canvas[0][0], 0, sizeof(int) * high * width);
	int i, j;
	//以下设定边框的数值
	for (i = 0; i < high; i++) {
		canvas[i][0] = -1;
		canvas[i][width - 1] = -1;
	}
	for (j = 0; j < width; j++) {
		canvas[0][j] = -1;
		canvas[high-1][j] = -1;
	}
	//以下初始化蛇头蛇身
	canvas[high / 2][width / 2] = 1;
	for (i = 0; i < 4; i++) {
		canvas[high / 2][width / 2 - i] = i+1;
	}
	//小蛇初始移动方向
	movedirection = 1;
	//食物初始化位置
	food_x = rand() % (high - 5) + 2;
	food_y = rand() % (width - 5) + 2;
	canvas[food_x][food_y] = -2;
	HideCursur();//隐藏光标
}
//显示画面
void show() {
	gotoxy(0, 0);//光标移动到原点位置，以下重画清屏
	int i, j;
	for (i = 0; i < high; i++) {
		for (j = 0; j < width; j++) {
			if (canvas[i][j] == 0) {
				printf(" ");
			}
			else if (canvas[i][j] == -1) {
				printf("#");
			}
			else if (canvas[i][j] == 1) {
				printf("@");
			}
			else if (canvas[i][j] > 1) {
				printf("*");
			}
			else if (canvas[i][j] == -2) {
				printf("F");
			}
		}
		printf("\n");
	}
	Sleep(150);
}
//与用户输入无关的更新
void updatewithoutinput() {
	movesnakebydirection();
}
//与用户输入有关的更新
void updatewithinput() {
	char input;
	if (_kbhit()) {
		input = _getch();
		if (input == 'a') {
			movedirection = 3;
		}
		if (input == 'w') {
			movedirection = 1;
		}
		if (input == 's') {
			movedirection = 2;
		}
		if (input == 'd') {
			movedirection = 4;
		}
	}
}
int main() {
	startup();
	while (1) {
		show();
		updatewithoutinput();
		updatewithinput();
	}
	return 0;
}